package com.ruoyi.gd.todo.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.IdUtil;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.helper.LoginHelper;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.gd.todo.domain.ZTodoFl;
import com.ruoyi.gd.todo.domain.bo.ZTodoFlBo;
import com.ruoyi.gd.todo.domain.vo.ZTodoFlVo;
import com.ruoyi.gd.todo.mapper.ZTodoFlMapper;
import com.ruoyi.gd.todo.service.IZTodoFlService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 待办内容分类Service业务层处理
 *
 * @author ruoyi
 * @date 2024-11-19
 */
@RequiredArgsConstructor
@Service
public class ZTodoFlServiceImpl implements IZTodoFlService {

    private final ZTodoFlMapper baseMapper;

    /**
     * 查询待办内容分类
     */
    @Override
    public ZTodoFlVo queryById(String id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询待办内容分类列表
     */
    @Override
    public TableDataInfo<ZTodoFlVo> queryPageList(ZTodoFlBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ZTodoFl> lqw = buildQueryWrapper(bo);
        Page<ZTodoFlVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询待办内容分类列表
     */
    @Override
    public List<ZTodoFlVo> queryList(ZTodoFlBo bo) {
        LambdaQueryWrapper<ZTodoFl> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ZTodoFl> buildQueryWrapper(ZTodoFlBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ZTodoFl> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getFlname()), ZTodoFl::getFlname, bo.getFlname());
        lqw.eq(StringUtils.isNotBlank(bo.getFlcode()), ZTodoFl::getFlcode, bo.getFlcode());
        lqw.eq(StringUtils.isNotBlank(bo.getFileauth()), ZTodoFl::getFileauth, bo.getFileauth());
        lqw.eq(StringUtils.isNotBlank(bo.getCreateId()), ZTodoFl::getCreateId, bo.getCreateId());
        return lqw;
    }

    /**
     * 新增待办内容分类
     */
    @Override
    public Boolean insertByBo(ZTodoFlBo bo) {
        ZTodoFl add = BeanUtil.toBean(bo, ZTodoFl.class);
        validEntityBeforeSave(add);
        String id = IdUtil.simpleUUID();
        add.setId(id);
        LoginUser loginUser = LoginHelper.getLoginUser();
        add.setCreateId(loginUser.getUserId()+"");
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改待办内容分类
     */
    @Override
    public Boolean updateByBo(ZTodoFlBo bo) {
        ZTodoFl update = BeanUtil.toBean(bo, ZTodoFl.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(ZTodoFl entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除待办内容分类
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
